<?php
include("../design/header.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script language="javascript" src="../scripts/rule.js"></script>

<link rel="stylesheet" type="text/css" href="../design/main.css" />
<title>Create Rules</title>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
</head>
<body>

<?php
$row_count = 1;
$temp_count=0;
$rule_meta_data_id = 0;

$operation =  $_GET["operation"];

if($operation!=NULL){ // update Operation
	$row_count = 0;
	$rule_meta_data_id = $_GET["rule_meta_data_id"];
	
	
		
	require_once("../../business/RetrieveRules.php");	
	
	$rulesObj = new RetrieveRules();
	$result = $rulesObj->viewRulesData($rule_meta_data_id);

	while ($row = mysql_fetch_array($result)){
		
		if($temp_count==0){
			$rule_desc = $row[0];
			$flag_id = $row[1];
		}

		$field_id[$temp_count] = $row[2];
		$logic_id[$temp_count] = $row[3];
		$rule_value[$temp_count] = $row[4];
		$temp_count++;	
		$row_count++;	
	}
	
			
}

require_once("../../business/RetrieveComboData.php");
$comboObj = new RetrieveComboData();


//print_r($col);
?>

<table id="mainTable" width="100%" class="mainTableHeight1024x768" border="0" cellpadding="0" cellspacing="0">
<tr>
    <td width="50" valign="top" class="LeftBgColor">&nbsp;<?php include("../design/leftMenu.php"); ?>
	</td>
    <td valign="top">


<table id="table1" name="table1" align="center" cellpadding="0" cellspacing="0" border="0" >
<form id="form1" name="form1" method="post" action="../../controller/ControlManagement.php?param=rules">


	    <tr>
    	<td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
    </tr>
        <tr>
    	<td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
    </tr>
<?php
$param =  $_GET["insert"];
if($param!=NULL){
	if($param=="true") {
?>
    <tr>
    	<td colspan="8" class="Labels">Data Successfully Inserted</td>
    </tr>

<?php
	
    }
	else if($param=="false") {
?>
    <tr>
    	<td colspan="8" class="Labels"><span class="style1">Data could not be inserted !!</span></td>
    </tr>

<?php
	
    }
}
?>

  <tr>
    	<td colspan="8" class="TableHeader">Create Rules</td>
     
    </tr>
   
    <tr>
        <td ><input type="hidden" id="criteria_counter" name="criteria_counter" value='<?php echo $row_count; ?>'></td>
        <td ><input type="hidden" id="rule_meta_data_id" name="rule_meta_data_id" value='<?php echo $rule_meta_data_id;?>'></td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
    </tr>
   
    <tr>
    	<td class="Labels">Rule Description</td>
        <td  ><input name="txt_rule_name" type="text"  class="InputText" size="32" value='<?php if(operation!=NULL) { echo $rule_desc;} ?>' /></td>
      <td class="Labels">Flag</td>
        <td  >

        <select name="combo_flag" class="InputComboList">
	        	
	 <?php 
				
			  	if(operation!=NULL) {  
				 	//$flagData = new RetrieveComboData("select flag_id, description from flag",$flag_id);
					echo $comboObj->getData("select flag_id, description from ota_flag",$flag_id);
				 }else {
					// $flagData = new RetrieveComboData("select flag_id, description from flag",0);
					
					 echo $comboObj->getData("select flag_id, description from ota_flag",0);
				 }
				 
					// echo $flagData->getData();		 
			 ?>
        </select>   
	
        
         </td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
    </tr>
    
    <tr>
    	<td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
    </tr>
    
   
   	<?php
//		$check= $row_count;
//		if($operation!=NULL) {  //update
//			$check = $row_count;
//		}
		
    	for($i=0;$i<$row_count;$i++){
	
	?>		
	
    <tr id='rule<?php echo $i ?>'>
    	<td class="Labels" >Field</td>
        <td>
        <select name="combo_field[]" id='combo_field<?php echo $i;?>' class="InputComboList">
          <?php 
		  	if(operation!=NULL) {  
				//  $fieldData = new RetrieveComboData("select field_id, description from field",$field_id[$i]);
				 echo $comboObj->getData("select field_id, description from ota_field",$field_id[$i]);
			 }else {
			 	// $fieldData = new RetrieveComboData("select field_id, description from field",0);
				 echo $comboObj->getData("select field_id, description from ota_field",0);
			 } 
			  //echo $fieldData->getData(); 
		  ?>
        </select></td>
       
   	  <td class="Labels" >Logic</td>
      
        <td>
        <select name="combo_logic[]"  id='combo_logic<?php echo $i;?>' class="InputComboList">
	        
        	 <?php 
			 	if(operation!=NULL) {  
				 	//$logicData = new RetrieveComboData("select logic_id, description from logic",$logic_id[$i]);
					echo $comboObj->getData("select logic_id, description from ota_logic",$logic_id[$i]);
				 }else {
				 	//$logicData = new RetrieveComboData("select logic_id, description from logic",0);
					echo $comboObj->getData("select logic_id, description from ota_logic",0);
				 }
				// echo $logicData->getData(); 
			 ?>
             
        </select>      
        </td>

    
    
    	<td class="Labels" >Value</td>
        <td  >
        <input type="text" name="text_value[]" id='text_value<?php echo $i;?>' class="InputText" value='<?php if(operation!=NULL) { echo $rule_value[$i];} ?>' ></td>
        <td><input type="button" name="btn_addCriteriaRow" id="btn_addCriteriaRow" value="Add" onClick="addCriteriaRow('table1');" class="InputButton"></td>
        <?php
        	if($i>0){
				?>
                
                <td>
                <input type="button" name='rule<?php echo $i;?>_btn' id='rule<?php echo $i;?>_btn' value="Remove" onClick="removeCriteriaRow('table1',this);" class="InputButton">
                </td>
				<?php
			}
		?>
        <td ></td>
    </tr>
	<?php
    	}// end of for
	
	?>
    <tr>
    	<td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
        <td >&nbsp;</td>
    </tr>
    
    <tr>    	
        <td  colspan="8" align="center">
        	<?php
            	if($operation!=NULL){
				
				?>
        	<input name="btn_submit"  type="button"  size="40" class="InputButton" value="Update" onClick="submitNow('form1','update');"/>        
                <?php
				} else {
			?>
        <input name="btn_submit" type="button" size="40" class="InputButton" value="Submit" onClick="submitNow('form1','insert');"/>
        	<?php
            	}
			?>
        </td>
    </tr>
  </form>  
</table>
    </td>
 </tr>   
</table>


</body>
</html>